package com.example.myapp_iphoneicon;

import android.app.Application;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.io.InputStream;

import com.example.demoex.R;

public class BitmapManager {
    public static Runnable sOnLowMemory;

    public static Bitmap createInvalidBitmap() {
        return Bitmap.createBitmap(1, 1, Config.ARGB_8888);
    }

    public static Bitmap copy(Bitmap bitmap, Bitmap.Config config,
            boolean isMutable) {
        try {
            return bitmap.copy(config, isMutable);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return createInvalidBitmap();

        }
    }

    public static Bitmap createBitmap(int paramInt1, int paramInt2,
            Bitmap.Config paramConfig) {
        try {
            return Bitmap.createBitmap(paramInt1, paramInt2, paramConfig);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return createInvalidBitmap();
        }
    }

    public static Bitmap createBitmap(Bitmap paramBitmap, int paramInt1,
            int paramInt2, int paramInt3, int paramInt4, Matrix paramMatrix,
            boolean paramBoolean) {
        try {
            return Bitmap.createBitmap(paramBitmap, paramInt1, paramInt2,
                    paramInt3, paramInt4, paramMatrix, paramBoolean);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return createInvalidBitmap();
        }
    }

    public static Bitmap createCoverableBitmap(Bitmap paramBitmap,
            int paramInt1, int paramInt2, boolean paramBoolean) {
        int paramOne = paramInt1;
        int paramTwo = paramInt2;
        if (paramBitmap.getWidth() < paramInt1) {
            paramTwo = (int) (paramBitmap.getHeight() * (paramInt1 / paramBitmap
                    .getWidth()));
        }
        if (paramTwo < paramInt2) {
            paramOne = (int) (paramBitmap.getWidth() * (paramInt2 / paramBitmap
                    .getHeight()));
        }
        return createScaledBitmap(paramBitmap, paramOne, paramTwo, paramBoolean);
    }

    public static Bitmap createScaledBitmap(Bitmap paramBitmap, int paramInt1,
            int paramInt2, boolean paramBoolean) {
        try {
            return Bitmap.createScaledBitmap(paramBitmap, paramInt1, paramInt2,
                    paramBoolean);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return createInvalidBitmap();
        }
    }

    public static Bitmap decodeByteArray(byte[] data) {
        try {
            return BitmapFactory.decodeByteArray(data, 0, data.length);
        } catch (OutOfMemoryError ex) {
            gc();
            return createInvalidBitmap();
        }
    }

    public static Bitmap decodeFile(String paramString,
            BitmapFactory.Options paramOptions) {
        try {
            return BitmapFactory.decodeFile(paramString, paramOptions);
        } catch (OutOfMemoryError ex) {
            gc();
            return createInvalidBitmap();
        }
    }

    public static Bitmap decodeStream(InputStream is, BitmapFactory.Options opts) {
        try {
            return BitmapFactory.decodeStream(is, null, opts);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return createInvalidBitmap();
        }
    }

    private static void gc() {
        if (sOnLowMemory != null)
            sOnLowMemory.run();
        System.gc();
    }

/*    public static Bitmap getBitmap(int paramInt) {
        if (Application.sContext != null) {
            Resources localResources = Application.sContext.getResources();
            Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
            return getBitmap(localResources, paramInt, localConfig);
        } else {
            return createInvalidBitmap();
        }
    }*/

    public static Bitmap getWallpaperIconShadow(Context context) {
//        if (mBitmapWallpaperIconShadow == null) {
            Bitmap localBitmap = BitmapManager.getBitmap(context,
                    R.drawable.wallpaper_icon_shadow);
//            mBitmapWallpaperIconShadow = localBitmap;
//        }
        return localBitmap;
    }
    
    public static Bitmap getDockbarIconShadow(Context context) {
//        if (mBitmapDockbarIconShadow == null) {
            Bitmap localBitmap = BitmapManager.getBitmap(context,
                    R.drawable.dockbar_icon_shadow);
//            mBitmapDockbarIconShadow = localBitmap;
//        }
        return localBitmap;
    }
    
    
    public static Bitmap getBitmap(Context paramContext, int paramInt) {
        Resources localResources = paramContext.getResources();
        Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
        return getBitmap(localResources, paramInt, localConfig);
    }

    public static Bitmap getBitmap(Context paramContext, int paramInt,
            Bitmap.Config paramConfig) {
        return getBitmap(paramContext.getResources(), paramInt, paramConfig);
    }

    public static Bitmap getBitmap(Resources paramResources, int paramInt,
            Bitmap.Config paramConfig) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inPreferredConfig = paramConfig;
        try {
            return BitmapFactory.decodeResource(paramResources, paramInt);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return createInvalidBitmap();
        }
    }

    public static Bitmap getBitmap(Drawable d) {
        Bitmap bitmap = null;
        if ((d instanceof BitmapDrawable)) {
            bitmap = ((BitmapDrawable) d).getBitmap();
            return bitmap;
        } else {
            bitmap = createBitmap(d.getIntrinsicWidth(),
                    d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            d.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            d.draw(new Canvas(bitmap));
        }

        return bitmap;
    }

    public static Drawable getDrawable(Context paramContext, int paramInt) {
        return getDrawable(paramContext.getResources(), paramInt);
    }

    public static Drawable getDrawable(Resources paramResources, int paramInt) {
        try {
            return paramResources.getDrawable(paramInt);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return paramResources.getDrawable(paramInt);
        }
    }

    public static Drawable getWallpaperDrawable(Context paramContext) {
        try {
            return WallpaperManager.getInstance(paramContext).getFastDrawable();
        } catch (OutOfMemoryError localOutOfMemoryError) {
            gc();
            return WallpaperManager.getInstance(paramContext).getFastDrawable();
        }
    }
}
